/* eslint-disable no-unused-vars */
import namespace from "../../namespace";

/**
 * 单排绘制座椅
 */
class RowSeatPolicy extends namespace.policy.canvas.SeatPolicy {
  NAME = `${namespace.namespace}.policy.canvas.RowSeatPolicy`;

  constructor() {
    super();
  }

  /**
   * @inheritdoc
   * @param {*} x
   * @param {*} y
   */
  drawFeakeSeats(x, y) {
    let count = this.calcSeatTotla(this.startPoint.distance({ x, y }));

    let overage = this.seats.length - count;

    // 删除多余图形
    if (overage >= 0) {
      new Array(overage).fill("").forEach(() => {
        let shape = this.seats.pop();
        shape.setCanvas(null);
      });
    }

    new Array(count).fill("").forEach((v, i) => {
      // 角度
      let radio = Math.atan2(y - this.startPoint.y, x - this.startPoint.x);
      // 半径
      let radius = (this.seatDiameter + this.seatSpace) * (i + 1);
      // 坐标点计算
      let newX = Math.floor(radius * Math.cos(radio) + this.startPoint.x);
      let newY = Math.floor(radius * Math.sin(radio) + this.startPoint.y);

      let shape = this.seats[i];
      if (!shape) {
        shape = new namespace.shape.SeatCircle({
          diameter: this.seatDiameter,
          x: 0,
          y: 0,
          alpha: 0.5
        });

        shape.setCanvas(this.canvas);
        this.seats.push(shape);
      }

      shape.setCenter(newX, newY);
    });

    // 总数 + 起点个数
    return count + 1;
  }

  /**
   * @inheritdoc
   * @param {*} distance
   */
  calcSeatTotla(distance) {
    const halfW = this.seatDiameter / 2;

    if (distance <= halfW) {
      return 0;
    }

    const count = (distance - halfW) / (this.seatDiameter + this.seatSpace);

    return Math.floor(Math.round(count));
  }
}

namespace.policy.canvas.RowSeatPolicy = RowSeatPolicy;
